let answersApp = new Vue({
    el: '#answersApp',
    data:{
        answers: []
    },
    methods:{
        answerSolved(answerId,answer){
            //判断答案是否已经被采纳过了
            if(answer.acceptStatus == 1){
                alert('问题已经被采纳过了!')
                return;
            }
            //发送 请求采纳
            $.ajax({
                url: "/faq/v1/answers/"+answerId+"/solved",
                method: 'get',
                success: function(r){
                    console.log(r)
                    if(r.code == ACCEPTED){
                        alert(r.message)
                        answer.acceptStatus = 1;
                    }else {
                        alert(r.message)
                    }
                }
            })

        },
        updateComment:function(commentId,answerId,index,comments){
            //获取评论内容
            let textarea = $("#editCommemt" + commentId + " textarea");
            let content = textarea.val();
            //console.log(commentId +"：" + content);
            if(!content){
                return;
            }
            let form = {
                answerId: answerId,
                content: content
            }

            $.ajax({
                url: "/faq/v1/comments/"+commentId+"/update",
                method: 'post',
                data: form,
                success: function(r){
                    //console.log(r)
                    //r.data 是修改后的对象comment
                    //将修改后的内容绑定给Vue,comments是评论数组， index是要修改的元素下标
                    Vue.set(comments,index,r.data)
                    //关闭下拉框
                    $("#editCommemt" + commentId).collapse("hide");

                }
            })


        },
        removeComment:function(commentId,index,comments){
          //alert(commentId);
            if(!commentId){
                return;
            }
            $.ajax({
                url: '/faq/v1/comments/'+commentId+'/delete',
                method: 'get',
                success:function(r){
                    console.log(r)
                    if(r.code == GONE){
                        //alert(r.message)
                        //js提供了splice（index，num）  index从哪个位置开始删除，num删除几个元素
                        comments.splice(index,1);
                    }else {
                        alert(r.message)
                    }
                }
            })
        },
        postComment:function(answerId){
            //console.log(answerId);
            let textarea = $("#addComment"+answerId+" textarea");
            let content = textarea.val();
            //console.log(content)
            //数据包装
            if(!content){
                return;
            }
            let form = {
                answerId: answerId,
                content: content
            }
            //发送请求
            $.ajax({
                url: "/faq/v1/comments",
                method: 'post',
                data: form,
                success:function(r){
                    if(r.code == CREATED){
                        //alert(r.message);
                        //获取新增评论对象
                        textarea.val('')//清空评论文本域中的内容
                        let comment = r.data;
                        //获取所有的回答
                        let answers = answersApp.answers;
                        for(let i=0;i<answers.length;i++){
                            if(answers[i].id == comment.answerId){
                                //找到对应回答的评论
                                //将其保存到当前回答评论的集合
                                answers[i].comments.push(comment);
                                break;
                            }
                        }

                    }else {
                        alert(r.message);
                    }
                }
            })

        },
        loadAnswers:function(){
            let questionId = location.search;
            if(!questionId){
                alert('必须获得问题ID')
                return;
            }
            questionId = questionId.substring(1);
            $.ajax({
                url: '/faq/v1/answers/question/' + questionId,
                method: 'get',
                success: function(r){
                    console.log(r)
                    if(r.code == OK){
                        answersApp.answers = r.data
                        answersApp.updateDuration();
                    } else {
                        alert(r.message)
                    }
                }
            })
        },
        updateDuration:function(){
            for(let i=0;i<this.answers.length;i++){
                addDuration(this.answers[i]);
            }
        }
    },
    created:function(){
        this.loadAnswers();
    }
})



let questionApp = new Vue({
    el: '#questionApp',
    data:{
        question:{}
    },
    methods:{
        loadQuestion:function(){
            //获取id 发送到后台   >>>  /v1/questions/150
            let questionId = location.search;
            //console.log(questionId); // ?150
            questionId = questionId.substring(1)
            //console.log(questionId); // 150
            if(!questionId){
                alert("必须指定问题ID");
                return;
            }
            $.ajax({
                url: '/faq/v1/questions/' + questionId,
                method: 'get',
                success:function(r){
                    if(r.code ==OK){
                        //console.log(r);
                        questionApp.question = r.data;
                        questionApp.updateDuration();
                    }else{
                        alert(r.message);
                    }

                }
            })
        },
        updateDuration:function(){
            //创建问题的时间 ms
            let createtime = new Date(this.question.createtime).getTime(); // 创建时间 -- ms
            //当前时间
            let now = new Date().getTime();
            let duration = now - createtime;
            if(duration < 60*1000){
                this.question.duration = '刚刚';
            }else if(duration < 60*60*1000){
                this.question.duration = (duration/1000/60).toFixed(0) + '分钟以前';
            }else if(duration < 24*60*60*1000){
                this.question.duration = (duration/1000/60/60).toFixed(0) + '小时以前';
            }else {
                this.question.duration = (duration/1000/60/60/24).toFixed(0) + '天以前';
            }
        }
    },
    created:function(){
        this.loadQuestion()
    }
})